package com.ruoyi.module.service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.module.domain.JdSynLog;
import com.ruoyi.module.domain.JdSynLogExample;
import com.ruoyi.module.mapper.JdSynLogMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@DS("bookmgr")
@Service
@RequiredArgsConstructor
public class JdSynLogService {
    private final JdSynLogMapper jdSynLogMapper;

    /**
     * 查询京东到家库存同步日志
     *
     * @param id 京东到家库存同步日志主键
     * @return 京东到家库存同步日志
     */
    public JdSynLog selectJdSynLogById(Long id) {
        return jdSynLogMapper.selectByPrimaryKey(id);
    }

    public int addLog(String bmbh, int size, String success, String memo) {
        JdSynLog log = new JdSynLog();
        log.setBmbh(bmbh);
        log.setSize(size);
        log.setResult("SUCCESS");
        log.setMemo(memo);
        return this.insert(log);
    }

    public int insert(JdSynLog jdSynLog) {
        jdSynLog.setCreateDate(LocalDateTime.now());
        jdSynLog.setTime(System.currentTimeMillis());
        return jdSynLogMapper.insert(jdSynLog);
    }

    /**
     * 查询京东到家库存同步日志列表
     *
     * @return 京东到家库存同步日志
     */
    public List<JdSynLog> selectJdSynLogList() {
        JdSynLogExample example = new JdSynLogExample();

        return jdSynLogMapper.selectByExample(example);
    }

    /**
     * 修改京东到家库存同步日志
     *
     * @param jdSynLog 京东到家库存同步日志
     * @return 结果
     */
    public int updateJdSynLog(JdSynLog jdSynLog) {
        return jdSynLogMapper.updateByPrimaryKeySelective(jdSynLog);
    }

    /**
     * 批量删除京东到家库存同步日志
     *
     * @param ids 需要删除的京东到家库存同步日志主键
     * @return 结果
     */
    public int deleteJdSynLogByIds(List<Long> ids) {
        JdSynLogExample example = new JdSynLogExample();
        example.or().andIdIn(ids);
        return jdSynLogMapper.deleteByExample(example);
    }

    /**
     * 删除京东到家库存同步日志信息
     *
     * @param id 京东到家库存同步日志主键
     * @return 结果
     */
    public int deleteJdSynLogById(Long id) {
        return jdSynLogMapper.deleteByPrimaryKey(id);
    }

    public JdSynLog selectLatestJdSynLog() {
        JdSynLogExample example = new JdSynLogExample();
        example.setOrderByClause(" id desc ");

        return jdSynLogMapper.selectOneByExample(example);
    }

    public int clearJdSynLogLessThanId(Long id) {
        JdSynLogExample example = new JdSynLogExample();
        example.or().andIdLessThan(id);

        return jdSynLogMapper.deleteByExample(example);
    }
}
